<?php

if( ! defined('DOCROOT') )
{
    require_once '/var/www/advancelab/chilik2/unittest/bootstrap.php';
}

class ControllerTest extends UnitTestCase
{
    function testingEmptyAction()
    {
        try
        {
            $cfController = new TestController(NULL, new Core\MVCRequest);
            $this->fail('Expected Exception not thrown');
        }
        catch( Exception $e )
        {
            $this->assertEqual("Controller TestController requires an action", $e->getMessage() );
        }
    }
    
    function testSetAction()
    {
        $cfController = new TestController( 'testAction', new Core\MVCRequest );
        
        /* $rc = new \ReflectionClass($cfController); */
        $prop = new \ReflectionProperty('TestController', 'action');
        $prop->setAccessible(TRUE);
        $this->dump($prop->getValue($cfController));
        
        $this->assertEqual("testAction", $prop->getValue($cfController) );
    }
    
    function testGetNonExistingVariables()
    {
        $req = new Core\MVCRequest();
        $req->variables = array('var1' => 42, 'var42' => 'Hello World');
        $cfController = new TestController('testAction', $req);
        
        try
        {
            $foo = $cfController->new;
            $this->fail('Expected exception not thrown.');
        }
        catch( Exception $e )
        {
            $this->assertEqual( "Abstraction\MvcController: Property new has not found !", $e->getMessage() );
        }
    }
    
    function testSetVariables()
    {
        $req = new Core\MVCRequest();
        $req->variables = array('var1' => 42, 'var42' => 'Hello World');
        $cfController = new TestController('testAction', $req);
        
        $this->assertEqual(42, $cfController->var1);
        $this->assertEqual('Hello World', $cfController->var42);
    }
    
    function testSetRequestVariables()
    {
        $req = new Core\MVCRequest();
        $req->variables = array('var1' => 42, 'var42' => 'Hello World');
        
        $cfController = new TestController('testCreateActionMethod', $req);
        $cfController->createResult();
        
        $this->assertEqual('testCreateActionMethod', $cfController->createResult() );
    }
    
    function testSetProperties()
    {
        $req = new Core\MVCRequest();
        $req->variables = array('var1' => 42, 'var42' => 'Hello World');
        $cfController = new TestController('testAction', $req);
        
        try
        {
            $cfController->new = 'fail !';
            $this->fail('Expected exception not thrown.');
        }
        catch( Exception $e )
        {
            $this->assertEqual( "Abstraction\MvcController: Property new of this class is read only !", $e->getMessage() );
        }
        
        try
        {
            $cfController->var1 = 'modified';
            $this->fail('Expected exception not thrown.');
        }
        catch( Exception $e )
        {
            $this->assertEqual( "Abstraction\MvcController: Property var1 of this class is read only !", $e->getMessage() );
        }
    }
    
    function testIssetProperties()
    {
        $req = new Core\MVCRequest();
        $req->variables = array('var1' => 42, 'var42' => 'Hello World');
        $cfController = new TestController('testAction', $req);
        
        $this->assertFalse(isset($cfController->notSet));
        $this->assertTrue(isset($cfController->var1));
    }
    
    function testRoutingInformation()
    {
        $req = new Core\MVCRequest();
        $req->variables = array('var1' => 42, 'var42' => 'Hello World');
        $cfController = new TestController('testAction', $req);
        $cfController->setRouter( new TestSimpleRouter( $req ) );
        
        $this->assertEqual( new TestSimpleRouter( $req ), $cfController->getRouter() );
    }
    
    function testCreateActionMethod()
    {
        $cfController = new TestController('test', new Core\MVCRequest() );
        $this->assertEqual( 'test', $cfController->testCreateActionMethod() );
        
        $cfController = new TestController('test_action', new Core\MVCRequest() );
        $this->assertEqual( 'testAction', $cfController->testCreateActionMethod() );
        
        $cfController = new TestController('testAction', new Core\MVCRequest() );
        $this->assertEqual( 'testAction', $cfController->testCreateActionMethod() );
        
        $cfController = new TestController('test_with_more_than_OneWord', new Core\MVCRequest() );
        $this->assertEqual( 'testWithMoreThanOneWord', $cfController->testCreateActionMethod() );
    }
    
}

